﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Xml;

namespace RSSR
{
    public static class ConfigFile
    {
        static string _RSSFeedsStore = "Feeds.xml";
        static IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        public static void CreateConfigFile()
        {
            //store.DeleteFile(_RSSFeedsStore);
            IsolatedStorageFileStream MyConfigFile = store.CreateFile(_RSSFeedsStore);

          
            XDocument x = new XDocument();
            
            x.Declaration = new XDeclaration("1.0", "UTF-8", "");
            x.Add(new XElement("MySavedFeeds", ""));
            x.Save(MyConfigFile);

            
            MyConfigFile.Close();


        }

        public static void AddRSSFeed(RSSFeed aFeed)
        {
            IsolatedStorageFileStream MyFile = store.OpenFile(_RSSFeedsStore, FileMode.Open, FileAccess.ReadWrite);
            
            MyFile.Position = 0;
            XDocument MyElement = XDocument.Load(MyFile);
            
            MyElement.Element("MySavedFeeds").Add(aFeed.GetXElement());
            
            MyFile.Position = 0;
            MyElement.Save(MyFile);
            
            MyFile.Close();
        }

        public static bool ConfigFileExists()
        {
            return store.FileExists(_RSSFeedsStore);
        }
    }
}
